$(function () {
  var cp_datagrid = $("#cp_datagrid");
  var cp_dialog = $("#cp_dialog");
  var cp_form = $("#cp_form");
  var insureInfoSn = $("#insureInfoSn");
  /*--------dialog2---*/
  var case_form = $("#case_form");
  var cp_title = $("#cp_title");
  var cp_dialog2 = $("#cp_dialog2");
  /*--------回选---*/
  var show_reporter = $("#show_reporter");
  var show_gender = $("#show_gender");
  var show_tel = $("#show_tel");
  var show_caseTime = $("#show_caseTime");
  var show_caseSpot = $("#show_caseSpot");
  var show_insureInfoSn = $("#show_insureInfoSn");
  var show_carNum = $("#show_carNum");
  var show_accidentResponsibility = $("#show_accidentResponsibility");
  var show_accidentDescription = $("#show_accidentDescription");
  var show_remarks = $("#show_remarks");

  cp_datagrid.datagrid({
    url: "/compensationInfo/query.do",
    fitColumns: true,
    fit: true,
    toolbar: "#cp_toolbar",
    striped: true,
    rownumbers: true,
    singleSelect: true,
    pagination: true,
    columns: [
      [
        {field: 'beneficiary', title: '受益人', width: 80},
        {field: 'accountNum', title: '收款账户', width: 80},
        {field: 'amount', title: '赔偿金额', width: 80},
        {
          field: 'insureInfoSn', title: '保单编号', width: 80, formatter: function (value,row) {
            return row.caseInfo ? row.caseInfo.insureInfoSn : "";
          }
        },
        {
          field: 'xxx', title: '案件详情', width: 80, formatter: function () {
            return '<a class="showCaseMsg">查看详情</a>';
          }
        },
        {
          field: 'compensationInfo', title: '赔损明细', width: 80, formatter: function () {
            return '<a class="showCaseInfo">查看明细</a>';
          }
        },
      ]]
  });

  $("#box").on("click", "a.showCaseInfo", function () {
    //获取当前选中的行
    var row = cp_datagrid.datagrid('getSelected');
    if (row.id) {
      $.get('/compensationInfo/selectById.do', {id: row.id}, function (data) {
        // 回显数据
        cp_form.form('load', data);
        cp_form.form('load', {insureInfoSn: row.caseInfo.insureInfoSn});
      })
      //打开弹出框
      cp_dialog.dialog('open')
    }
  }).on("click", "a.showCaseMsg", function () {
    //获取当前选中的行
    var row = cp_datagrid.datagrid('getSelected');
    show_reporter.text(row.caseInfo.reporter)
    show_gender.text(row.caseInfo.gender ? "男": "女")
    show_tel.text(row.caseInfo.tel)
    show_caseTime.text(row.caseInfo.caseTime)
    show_caseSpot.text(row.caseInfo.caseSpot)
    show_insureInfoSn.text(row.caseInfo.insureInfoSn)
    show_carNum.text(row.caseInfo.carNum)
    show_accidentResponsibility.text(row.caseInfo.accidentResponsibility)
    show_accidentDescription.text(row.caseInfo.accidentDescription)
    show_remarks.text(row.caseInfo.remarks)
    openSetTitle("查看报案信息")
  })
  var css = {
    width: '100%',
    height: '100px'
  };
  function clearForm() {
    case_form.form('clear');
    cp_dialog2.modal('hide');
  }
  function openSetTitle(str) {
    cp_title.html(str);
    cp_dialog2.modal('show');
  }

  cp_dialog.dialog({
    title: '赔损明细',
    draggable: 'true',
    width: 300,
    height: 385,
    buttons: "#form_btns",
    closed: "true",
    onClose: function () {
      //清空表单数据
      cp_form.form('clear');
    }
  })

  //使用一个对象统一管理事件的方法
  var methodObj = {
    /*查询*/
    query: function () {
      //获取查询条件的值
      var value = $("#keyword").textbox('getValue');

      cp_datagrid.datagrid('load', {
        keyword: value
      })
    },
    /*刷新按钮*/
    refresh: function () {
      //关闭弹出框
      cp_datagrid.datagrid('reload');
    },
    cancel: function () {
      //关闭弹出框
      cp_dialog.dialog('close');
    }
  };

  //统一绑定事件
  $("a[data-cmd]").click(function () {
    var methodName = $(this).data("cmd");
    methodObj[methodName]();
  });
});